// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

var touch_event_param = {};
var globalEnum = require("./globalEnum");

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    getAction: function() {
        return {
            rotateAction: {
                action: cc.repeatForever(cc.rotateBy(3, 360)),
                flag: false
            },
            moveAction: {
                action: cc.moveTo(1.5, cc.v2(200, -200)),
                flag: false
            }
        };
    },

    touch_move: function(t) {
        let pos = t.getLocation();
        console.log(pos,"---",pos.x,"---",pos.y);
        let delta = t.getDelta();
        console.log(delta,"---",delta.x,"---",delta.y);
        this.node.x += delta.x;
        this.node.y += delta.y;
    },

    on_key_down: function(event) {
        console.log(event.keyCode);
        switch (event.keyCode) {
            case (cc.macro.KEY.j):
                console.log('j');
                break;
            case (cc.macro.KEY.space):
                if (!touch_event_param.rotateActionFlag) {
                    this.node.runAction(touch_event_param.rotateAction.action);
                    touch_event_param.rotateAction.flag = true;
                }
                break;
            case (cc.macro.KEY.enter):
                console.log('enter');
                break;
            case (cc.macro.KEY.num1):
                console.log('num1');
                break;
            case (49):
                console.log('1');
                break;
            default:
                console.log('沒有匹配的按鍵事件！');
                break;
        }
    },

    on_key_up: function(event) {
        switch (event.keyCode) {
            case (cc.macro.KEY.space):
                this.node.stopAction(touch_event_param.rotateAction.action);
                touch_event_param.rotateAction.flag = false;
                break;
            default:
                console.log('沒有匹配的按鍵事件！');
                break;
        }
    },

    moveAction: function() {
        let moveAction = touch_event_param.moveAction.action;
        if (!touch_event_param.moveAction.flag) {
            this.node.runAction(moveAction);
            touch_event_param.moveAction.flag = true;
        } else {
            touch_event_param.moveAction.flag = false;
            this.node.stopAction(touch_event_param.moveAction.action);
        }
    },

    onLoad () {
        // 動作初始化
        touch_event_param = this.getAction();
        console.log(touch_event_param);
        console.log(globalEnum);

        // 自定義事件監聽
        this.node.on(globalEnum.event.item1Move, this.moveAction, this);
        
        // 操作事件放在最後
        this.node.on(cc.Node.EventType.TOUCH_START, function(t) {
            console.log("觸摸了一下：", t);
            this.node.emit(globalEnum.event.item1Move);
            let item2 = cc.find(globalEnum.nodePath.item2);
            console.log(item2);
            item2.emit(globalEnum.event.item2Move);
        }, this);
        // this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touch_move, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.on_key_down, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.on_key_up, this);
        // this.node.on("fuck", function(event) {
        //     console.log(event.detail,"fuck!");
        // }, this);
        // this.node.emit("fuck", {detail : {bitch: "天海翼"}});
        // let e = new cc.Event.EventCustom("fuck", true);
        // e.detail = {bitch: "瀧澤蘿拉"};
        // this.node.dispatchEvent(e);
        // let pos3 = this.node.convertToNodeSpace(cc.v2(480,320));
        // console.log("item_pos3", pos3);
        // let pos4 = this.node.convertToNodeSpaceAR(cc.v2(480,320));
        // console.log("item_pos4", pos4);
        // let action02 = cc.moveBy(1, cc.v2(100, 100));
        // this.node.runAction(action02);
        // let rtAction01 = cc.rotateTo(1, 100);
        // this.node.runAction(rtAction01);
        // this.node.scale = 2;
        // let scAction01 = cc.scaleTo(1, 1.5);
        // this.node.runAction(scAction01);
        // console.log(this.node.position);
        // let jumpAction01 = cc.jumpTo(2, this.node.position.add(cc.v2(100,100)), 200, 4);
        // let fadeAction01 = cc.fadeOut(1);
        // this.node.runAction(cc.sequence(jumpAction01, cc.callFunc(function(){
        //     console.log("開始消失！");
        //     this.node.active = false;
        //     console.log("消失完畢！");
        //     cc.director.getScheduler().schedule(function() {
        //         this.node.active = true;
        //         console.log("臥槽，有動靜沒？！");
        //         this.node.opacity = 255;
        //     }, this, 0.4, 0, 0, false);
        // }.bind(this))));
        // console.log("老子叫牛逼！");
        // this.node.runAction(cc.sequence(fadeAction01, cc.callFunc(function() {
        //     console.log("漸變消失結束");
        // }.bind(this))));


    },

    start () {

    },

    // update (dt) {},
});
